<?php
/**
 * Created by PhpStorm.
 * User: FDG296
 * Date: 2017/8/2
 * Time: 16:42
 */
namespace backend\models;

use Yii;
use yii\db\ActiveRecord;

class GoodsCateRelation extends ActiveRecord{
    const STATUS_ENABLE = 1;
    const STATUS_DISABLE = 0;

    public static function tableName(){
        return '{{%goods_cate_relation}}';
    }

    public function rules(){
        return [
            ['use_flg','default','value'=>self::STATUS_DISABLE],
            ['creator','getCreator'],
            ['create_time','date']
        ];
    }

    /**
     * @获取创建人
     */
    public function getCreator(){
        $userInfo = \common\helps\tools::getloginUserInfo();
        if($userInfo){
            $userName = $userInfo->userName;
        }else{
            $userName = '';
        }
        return $userName;
    }
    
    public function getTagList(){
        $where = ['r.use_flg'=>1];
        $result = $this::find()->where($where)->alias('r')->select('r.id,t.tag_name,t.tag_alias,r.category_id')->
        leftJoin('j_goods_tag as t','r.tag_id = t.id')->orderBy('t.sort asc')->asArray()->all();
        $list = array();
        foreach($result as $value){
            $list[$value['category_id']][] = $value;
        }
        return $list;
    }
}